import 'package:car_web/model/car_entity.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:responsive_builder/responsive_builder.dart';

import 'home_provider.dart';

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (BuildContext context) => HomeProvider(),
      builder: (context, child) => _buildPage(context),
    );
  }

  Widget _buildPage(BuildContext context) {
    final provider = context.read<HomeProvider>();
    var deviceType = getDeviceType(MediaQuery.of(context).size);

    return Scaffold(
      body: SingleChildScrollView(
        child: Container(
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: provider.data
                .map((e) => Container(
                      width: 200,
                      margin: EdgeInsets.symmetric(horizontal: 10),
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          _buildTitle(e.carType),
                          e.carImg.startsWith('http')
                              ? Image.network(
                                  e.carImg,
                                  width: 200,
                                  height: 200,
                                )
                              : _buildTitle(e.carImg,height: 200),
                          _buildTitle(e.carScore),
                          _buildTitle(e.quotedPrice),
                          _buildTitle(e.guidePrice),
                          SizedBox(
                              width: 200,
                              height: 100,
                              child: Center(
                                child: Text(
                                  e.carAttributes,
                                  softWrap: true,
                                  maxLines: 3,
                                ),
                              )),
                        ],
                      ),
                    ))
                .toList(),
          ),
        ),
      ),
    );
  }

  SizedBox _buildTitle(String title, {double height = 50}) {
    return SizedBox(
        width: 200, height: height, child: Center(child: Text(title)));
  }

  _buildDrawer() {
    return Drawer(
      child: ListView(
        children: [],
      ),
    );
  }
}
